﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.Common.Model;
using Yipusoft.Common.BLL;
using Yipusoft.SixAcquaint.Model;
using Yipusoft.SixAcquaint.BLL;
using Yipusoft.Account.BLL;
using System.Collections.Generic;
using Yipusoft.Account.Model;

public partial class BasicBase_RoadSource_SearchRoad : System.Web.UI.Page
{
    private static string strSql = "1=1";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "200915") || Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "200900"))
            {
                BindProvince();
                BindCities(ddlProvinces.SelectedValue);
                BindDepartment("200900", "200911", "200912");
            }
            else
            {
                Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                return;
            }

        }
    }
    private void BindProvince()
    {
        //ddlProvinces.DataSource = CodeItem.GetList("ParentID='SS000000' ORDER BY SeqID");
        //ddlProvinces.DataTextField = "ItemName";
        //ddlProvinces.DataValueField = "ItemID";
        //ddlProvinces.DataBind();
        ListItem blankLI = new ListItem("", "");
        ddlProvinces.Items.Add(blankLI);
        foreach (CodeItemInfo item in CodeItem.GetList("ParentID='SS000000' ORDER BY SeqID"))
        {
            ListItem li = new ListItem(item.ItemName, item.ItemID);
            ddlProvinces.Items.Add(li);
        }
    }
    private void BindDepartment(string permissionAdmin, string permissionLevel, string permissionUnit)
    {
        if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionAdmin))
        {
            ListItem blankLI = new ListItem("--单位--", "");
            ddlDepartment.Items.Add(blankLI);

            foreach (DepartmentInfo department in Department.GetList("RIGHT(DepartmentID, 2) = '00'"))
            {
                ddlDepartment.Items.Add(new ListItem(department.FullName, department.DepartmentID));
            }
        }
        else
        {
            ddlDepartment.DataSource = GetDepartmentList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID, permissionLevel, permissionUnit);
            ddlDepartment.DataTextField = "FullName";
            ddlDepartment.DataValueField = "DepartmentID";
            ddlDepartment.DataBind();

        }
    }
    private List<DepartmentInfo> GetDepartmentList(string departmentID, string permissionLevel, string permissionUnit)
    {
        List<DepartmentInfo> departmentList = new List<DepartmentInfo>();
        if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionLevel) || Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionUnit))
            departmentList = Department.GetList("DepartmentID='" + departmentID.Substring(0, 4) + "000'");
        return departmentList;
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        lblInfo.Text = "";
        strSql = "1=1";
        if (txtRoad.Text != "")
            strSql += " and RoadName like '%" + txtRoad.Text + "%'";
        if (txTrend.Text != "")
            strSql += " and Trend like '%" + txTrend.Text + "%'";
        if (txtStartPoint.Text != "")
            strSql += " and StartPoint='" + txtStartPoint + "'";
        if (txtEndPoint.Text != "")
            strSql += " and EndPoint='" + txtEndPoint + "'";
        if (ddlRank.SelectedValue != "")
            strSql += " and Rank=" + ddlRank.SelectedValue;
        if (ddlCities.SelectedValue != "")
            strSql += " and RegionID='" + ddlCities.SelectedValue + "'";
        if (txtLength.Text != "")
            strSql += " and Length" + ddlCon.SelectedValue + txtLength.Text;
        if (ddlDepartment.SelectedValue != "")
            strSql += " and DepartmentID like '%" + ddlDepartment.SelectedValue.Substring(0, 4) + "%'";
        BindRoads(strSql, 8, 0);

    }
    protected void ddlProvinces_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindCities(ddlProvinces.SelectedValue);
    }

    private void BindCities(string province)
    {
        //ddlCities.DataSource = CodeItem.GetList("ParentID='"+province+"' Order by SeqID");
        //ddlCities.DataTextField = "ItemName";
        //ddlCities.DataValueField = "ItemID";
        //ddlCities.DataBind();
        ddlCities.Items.Clear();
        if (province.Trim() == "")
        {
            ListItem blankLI = new ListItem("", "");
            ddlCities.Items.Add(blankLI);
        }
        else
        {
            foreach (CodeItemInfo item in CodeItem.GetList("ParentID='" + province + "' Order by SeqID"))
            {
                ListItem li = new ListItem(item.ItemName, item.ItemID);
                ddlCities.Items.Add(li);
                foreach (CodeItemInfo subItem in CodeItem.GetList("ParentID='" + item.ItemID + "' Order by SeqID"))
                    ddlCities.Items.Add(new ListItem("\u3000" + subItem.ItemName, subItem.ItemID));
            }
        }
    }
    protected void gvRoads_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        RoadInfo roadDetails = Road.GetRoadByID(gvRoads.DataKeys[e.RowIndex].Value.ToString());
        if (roadDetails.DepartmentID != Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID)
        {

            lblInfo.Text = "只有" + roadDetails.DepartmentName + "单位内的用户方可删除" + roadDetails.RoadName + "的数据。";
        }
        else
        {
            if (Road.Delete(gvRoads.DataKeys[e.RowIndex].Value.ToString()))
            {
                BindRoads(ddlCities.SelectedValue, 8, (int)ViewState["PageIndex"]);
            }
        }
    }
    private void BindRoads(string strSql, int pageSize, int pageIndex)
    {
        // = Road.GetRoadsByRegionID(ddlCities.SelectedValue);
        if (!panPage.Visible)
            panPage.Visible = true;
        int totalCount = Road.GetRoadsCountByWhere(strSql);
        //pageIndex = pageIndex + 1;
        ViewState["PageCount"] = Convert.ToInt32(Math.Ceiling((double)totalCount / pageSize));
        if ((int)ViewState["PageCount"] < pageIndex)
            pageIndex = pageIndex - 1;
        ViewState["PageIndex"] = pageIndex;

        gvRoads.DataSource = Road.GetRoadsList(strSql, "RegionID", "asc", pageSize, pageIndex);
        //hfTot.Value = Road.GetRoadsByRegionID(ddlCities.SelectedValue).Count.ToString();
        gvRoads.DataBind();
        lbGridInfo.Text = "共有<b>" + totalCount.ToString() + "</b>条记录 第<b>" + ((int)ViewState["PageIndex"] + 1).ToString() + "</b>页 共<b>" + ViewState["PageCount"].ToString() + "</b>页 每页<b>" + pageSize.ToString() + "</b>条";
        this.btnFirst.Enabled = (int)ViewState["PageIndex"] > 0;
        this.btnPrev.Enabled = (int)ViewState["PageIndex"] > 0;
        this.btnNext.Enabled = (int)ViewState["PageIndex"] < (int)ViewState["PageCount"] - 1;
        this.btnLast.Enabled = (int)ViewState["PageIndex"] < (int)ViewState["PageCount"] - 1;
        BindJumpList();
    }
    protected void JumpList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindRoads(strSql, 8, int.Parse(JumpList.SelectedValue));
    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = 0;
        BindRoads(strSql, 8, (int)ViewState["PageIndex"]);
    }
    protected void btnPrev_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageIndex"] - 1;
        BindRoads(strSql, 8, (int)ViewState["PageIndex"]);
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageIndex"] + 1;
        BindRoads(strSql, 8, (int)ViewState["PageIndex"]);
    }
    protected void btnLast_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageCount"] - 1;
        BindRoads(strSql, 8, (int)ViewState["PageIndex"]);
    }
    private void BindJumpList()
    {
        JumpList.Items.Clear();
        ListItem li;
        for (int i = 0; i < (int)ViewState["PageCount"]; i++)
        {
            li = new ListItem();
            li.Text = (i + 1).ToString();
            li.Value = i.ToString();
            JumpList.Items.Add(li);
        }
        JumpList.SelectedIndex = JumpList.Items.IndexOf(JumpList.Items.FindByValue(ViewState["PageIndex"].ToString()));
    }
    protected void gvRoads_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (((int)ViewState["PageIndex"]) * gvRoads.PageSize + (e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }
}
